package com.example.demo.controller;


import com.example.demo.dto.PaginationDto;
import com.example.demo.dto.ProductDto;
import com.example.demo.dto.QueryProductDto;
import com.example.demo.dto.StatusDto;
import com.example.demo.model.Result;
import com.example.demo.service.ProductService;
import com.example.demo.utils.OssUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;


@RestController
@RequestMapping("/ProductController")
@AllArgsConstructor
public class ProductController {
    private OssUtil ossUtil;

    private ProductService productService;

//    public ProductController(ProductService productService) {
//        this.productService = productService;
//    }

    @GetMapping
    public Result getPageData(QueryProductDto queryProductDto, PaginationDto paginationDto) {
        return productService.getAllProducts(queryProductDto, paginationDto);
    }

    @PutMapping("/{id}")
    public Result changeStatus(@PathVariable Integer id, @RequestBody StatusDto statusDto) {
        statusDto.setId(id);
        return productService.changeStatus(statusDto);
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return productService.getById(id);
    }

    @PostMapping("/fileOss2")
    @ResponseBody
    public Result addImg(@RequestParam("file") MultipartFile[] fileList,
                         @RequestParam("name") String name,
                         @RequestParam("content") String content) throws Exception {
        ProductDto productDto = new ProductDto();
        productDto.setName(name);
        productDto.setContent(content);

        productService.addProduct(productDto,fileList);
        return Result.buildFail("success");
    }
    @GetMapping("/d/{id}")
    public Result delById(@PathVariable("id") Integer id) {
        if(productService.delById(id)){
            return Result.buildSuccess();
        }
        return Result.buildFail("删除失败",-1);
    }

    /**
     * 修改信息
     */
    @PostMapping("/e/{id}")
    @ResponseBody
    public Result updateProduct(@PathVariable("id") Integer id,
                            @RequestBody QueryProductDto formdata ) throws Exception {
        System.out.println(id);
        System.out.println(formdata);

        productService.updateProduct(formdata);


        return Result.buildFail("success");
    }

    //上传文件
    @PostMapping("/upload/fileOss")
    @ResponseBody
    public Result uploadImg(@RequestParam("file") MultipartFile[] fileList) throws Exception {
        ArrayList<String> urlList = new ArrayList<>();
        for (MultipartFile multipartFile : fileList) {
            String url = ossUtil.uploadFileAvatar(multipartFile);
            urlList.add(url);
            System.out.println(url);
        }
        return Result.buildSuccess(urlList);
    }

}
